'use strict';

angular.module('CBO').controller('RoutingController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', 'PublicService', 'PublicEnum', 'ProduceWorkEnum', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, PublicService, PublicEnum, ProduceWorkEnum, $location, Authentication) {
        $scope.DeliveryModeEnum = ProduceWorkEnum.DeliveryMode;
        $scope.PushPullModeEnum = ProduceWorkEnum.PushPullMode;

        PublicService.getExtendEnum('ItemCategory', function(extendEnum){
            $scope.ItemCategoryEnum = extendEnum;
        });

        $scope.btnSave = function(){
            if($scope.Module.Routing.FocusedRecord.Code === undefined || 
                $scope.Module.Routing.FocusedRecord.Code.trim() === ""){
                PublicFunction.alert("编码不能为空！", "工艺路线");
                return;
            }
            if($scope.Module.Routing.FocusedRecord.Name === undefined || 
                $scope.Module.Routing.FocusedRecord.Name.trim() === ""){
                PublicFunction.alert("名称不能为空！", "工艺路线");
                return;
            }
            if($scope.Module.Routing.FocusedRecord.ItemMaster === undefined || 
                $scope.Module.Routing.FocusedRecord.ItemMaster == null || 
                $scope.Module.Routing.FocusedRecord.ItemMaster._id === null ||
                $scope.Module.Routing.FocusedRecord.ItemMaster._id === ""){
                PublicFunction.alert("料品不能为空！", "工艺路线");
                return;
            }

            if($scope.Module.Operation.Records.length > 0){
                for(var i=0; i<$scope.Module.Operation.Records.length; i++){
                    if($scope.Module.Operation.Records[i].Sequence === undefined || 
                        $scope.Module.Operation.Records[i].Sequence === null){
                        PublicFunction.alert("工序" + $scope.Module.Operation.Records[i].Sequence + "：序号不能为空！", "工序");
                        return;
                    }
                    if($scope.Module.Operation.Records[i].Name === undefined || 
                        $scope.Module.Operation.Records[i].Name.trim() === ""){
                        PublicFunction.alert("工序" + $scope.Module.Operation.Records[i].Sequence + "：名称不能为空！", "工序");
                        return;
                    }

                    for(var j=0; j<$scope.Module.Operation.Records.length; j++){
                        if(i !== j && $scope.Module.Operation.Records[i].Sequence === $scope.Module.Operation.Records[j].Sequence){
                            PublicFunction.alert("序号" + $scope.Module.Operation.Records[i].Sequence.toString() + "：不能重复！", "工序");
                            return;
                        }
                    }
                }
            }
            else{
                PublicFunction.alert("工序行不能为空！", "工序");
                return;
            }

            if($scope.Module.Routing.FocusedRecord.RowStatus !== PublicEnum.RowStatus.UnChange){
                $scope.Module.Routing.save(function(){
                    for(var i=0; i<$scope.Module.Operation.Records.length; i++){
                        if($scope.Module.Operation.Records[i].RowStatus === PublicEnum.RowStatus.Insert){
                            $scope.Module.Operation.Records[i].Routing = $scope.Module.Routing.FocusedRecord._id;
                        }
                    }
                    $scope.Module.Operation.QueryParameter.Condition = {Routing: $scope.Module.Routing.FocusedRecord._id};
                    $scope.Module.Operation.save();
                });
            }
            else{
                for(i=0; i<$scope.Module.Operation.Records.length; i++){
                    if($scope.Module.Operation.Records[i].RowStatus === PublicEnum.RowStatus.Insert){
                        $scope.Module.Operation.Records[i].Routing = $scope.Module.Routing.FocusedRecord._id;
                    }
                }
                $scope.Module.Operation.QueryParameter.Condition = {Routing: $scope.Module.Routing.FocusedRecord._id};
                $scope.Module.Operation.save();
            }
        };
        $scope.btnCancel = function(){
            $scope.Module.Routing.cancel(function(){
                loadOperation();
            });
        };
        $scope.btnAdd = function(){
            $scope.Module.Routing.add();
            $scope.Module.Routing.FocusedRecord.IsEffective = true;
            $scope.Module.Operation.clearRecords();
        };
        $scope.btnDelete = function(){
            if($scope.Module.Routing.FocusedRecord.RowStatus !== PublicEnum.RowStatus.Insert){
                PublicService.delete('Operation', {Routing: $scope.Module.Routing.FocusedRecord._id}, function(){
                    $scope.Module.Operation.Records = [];
                    $scope.Module.Routing.delete();
                });
            }
            else{
                $scope.Module.Routing.delete();
                $scope.Module.Operation.Records = [];
            }
        };
        $scope.btnList = function(){
            $location.url('/index/Routing/List');
        };
        $scope.btnFirstPage = function(){
            $scope.Module.Routing.firstPage(function(){
                //loadOperation();
            });
        };
        $scope.btnPrePage = function(){
            $scope.Module.Routing.prePage(function(){
                //loadOperation();
            });
        };
        $scope.btnNextPage = function(){
            $scope.Module.Routing.nextPage(function(){
                loadOperation();
            });
        };
        $scope.btnLastPage = function(){
            $scope.Module.Routing.lastPage(function(){
                //loadOperation();
            });
        };
        $scope.addRow = function(){
            var maxLineValue = 0;
            for(var i=0; i<$scope.Module.Operation.Records.length; i++){
                if($scope.Module.Operation.Records[i].Sequence !== "" && 
                    $scope.Module.Operation.Records[i].Sequence !== undefined && 
                    $scope.Module.Operation.Records[i].Sequence > maxLineValue)
                {
                    maxLineValue = $scope.Module.Operation.Records[i].Sequence;
                }
            }
            $scope.Module.Operation.FocusedRecord.Sequence = maxLineValue + 10;
            $scope.Module.Operation.FocusedRecord.Routing = $scope.Module.Routing.FocusedRecord._id;
            $scope.Module.Operation.refreshDataGridRows();
        };
        $scope.cellValueChange = function(columnName, newValue, oldValue){
            if(columnName === "Work.Code"){
                if(newValue.length > 0){
                    $scope.Module.Operation.FocusedRecord.ConnectArea._id = newValue[0].ConnectArea._id;
                    $scope.Module.Operation.FocusedRecord.ConnectArea.Code = newValue[0].ConnectArea.Code;
                    $scope.Module.Operation.FocusedRecord.ConnectArea.Name = newValue[0].ConnectArea.Name;
                    $scope.Module.Operation.FocusedRecord.IsOnlineControl = newValue[0].IsOnlineControl;
                    $scope.Module.Operation.FocusedRecord.IsOfflineControl = newValue[0].IsOfflineControl;
                    $scope.Module.Operation.FocusedRecord.IsCompletePoint = newValue[0].IsCompletePoint;
                    $scope.Module.Operation.FocusedRecord.IsQCpoint = newValue[0].IsQCpoint;
                    $scope.Module.Operation.FocusedRecord.IsDrive = newValue[0].IsDrive;
                }
                else{
                    $scope.Module.Operation.FocusedRecord.ConnectArea = null;
                    $scope.Module.Operation.FocusedRecord.IsOnlineControl = false;
                    $scope.Module.Operation.FocusedRecord.IsOfflineControl = false;
                    $scope.Module.Operation.FocusedRecord.IsCompletePoint = false;
                    $scope.Module.Operation.FocusedRecord.IsQCpoint = false;
                    $scope.Module.Operation.FocusedRecord.IsDrive = false;
                }
                $scope.Module.Operation.refreshDataGridRows();
            }
        };

        var loadOperation = function(){
            if($scope.Module.Routing.FocusedRecord !== null && 
                $scope.Module.Routing.FocusedRecord.RowStatus !== PublicEnum.RowStatus.Insert){
                //$scope.Module.Operation.QueryParameter.Condition = {Routing: $scope.Module.Routing.FocusedRecord._id};
                $scope.Module.Operation.load();
            }
            else{
                $scope.Module.Operation.Records = [];
            }            
        };

        //初始化
        PublicModule.initModule($scope, 'Routing');
        $scope.Module.Routing.QueryParameter.Populate = {path: 'ItemMaster', select: 'Code Name'};
        PublicModule.initModule($scope, 'Operation');
        $scope.Module.Operation.PageSize = 15;
        $scope.Module.Operation.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.Operation.QueryParameter.Populate = [{path: 'Work', select: 'Code Name'}, {path: 'ConnectArea', select: 'Code Name'}];
        $scope.Module.Operation.QueryParameter.OrderBy = {Sequence: 1};
        $scope.Module.Operation.createRelation($scope.Module.Routing, $scope);

        if($stateParams.id) {
            $scope.Module.Routing.findById($stateParams.id, function(record){
                $scope.Module.Routing.FocusedRecord = record;
                //loadOperation();
            });
        }
        else{
            $scope.btnAdd();
        }
    }
]);
